#import "RNNavigationArrowButtonCell.h"
#import "NSShadow+MCAdditions.h"
#import "NSBezierPath+MCAdditions.h"

static NSShadow *arrowDropShadow;
static NSGradient *pressedGradient;
static NSShadow *pressedInnerShadow;
static NSColor *navBarHighlightColor;
static NSColor *navBarStrokeColor;

@implementation RNNavigationArrowButtonCell

+(void)initialize {
    arrowDropShadow = [[NSShadow alloc] initWithColor:[NSColor colorWithCalibratedWhite:1.0f alpha:1.0f] offset:NSMakeSize(0.0f, -1.0f) blurRadius:1.0f];
    pressedGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.4f alpha:1.0f] endingColor:[NSColor colorWithCalibratedWhite:0.57f alpha:1.0f]];
    pressedInnerShadow = [[NSShadow alloc] initWithColor:[NSColor colorWithCalibratedWhite:0.2f alpha:1.0f] offset:NSZeroSize blurRadius:4.0f];
    navBarHighlightColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.5];
    navBarStrokeColor = [NSColor colorWithCalibratedWhite:0.1 alpha:0.5];
}

-(void)drawButtonNormalInFrame:(NSRect)cellFrame {
    CGFloat maxX = NSMaxX(cellFrame) - 0.5;
    CGFloat minX = NSMinX(cellFrame) + 0.5;
    CGFloat maxY = NSMaxY(cellFrame);
    CGFloat minY = NSMinY(cellFrame);
    [navBarStrokeColor setStroke];
    [NSBezierPath strokeLineFromPoint:NSMakePoint(maxX, minY) toPoint:NSMakePoint(maxX, maxY)];
    [navBarHighlightColor setStroke];
    [NSBezierPath strokeLineFromPoint:NSMakePoint(minX, minY) toPoint:NSMakePoint(minX, maxY)];
}

-(void)drawButtonPressedInFrame:(NSRect)cellFrame {
    [pressedGradient drawInRect:cellFrame angle:90];
    [[NSBezierPath bezierPathWithRect:cellFrame] fillWithInnerShadow:pressedInnerShadow];
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    if ([self isHighlighted])
        [self drawButtonPressedInFrame:cellFrame];
    else 
        [self drawButtonNormalInFrame:cellFrame];
    [NSGraphicsContext saveGraphicsState];
    [arrowDropShadow set];
    [self drawImage:[self image] withFrame:cellFrame inView:controlView];
    [NSGraphicsContext restoreGraphicsState];
}

@end
